<?php

use Illuminate\Database\Seeder;

class WastTypeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $iwDetailAll = DB::table('iw_detail')->select()->get()->toArray();
        foreach ($iwDetailAll as $iwDetail) {
            $iwDetailId = $iwDetail -> id;
            $masterUnit = DB::table('master_data')->where('master_id',$iwDetail -> Predetermined_quantity_unit)->select()->get()->toArray();
            $masterUnitDis = DB::table('master_data')->where('master_id',$iwDetail -> Predetermined_quantity_unit_Dispose)->select()->get()->toArray();
            $unit1 = '';
            $unit2 = '';
            if (!empty($masterUnit)) {
                $unit1 = $masterUnit[0]->master_data;
            }
            if (!empty($masterUnitDis)) {
                $unit2 = $masterUnitDis[0]->master_data;
            }
            DB::table('iw_detail')->where('id','=',$iwDetailId)->update(
                [
                    'Predetermined_quantity_unit' => $unit1,
                    'Predetermined_price_unit' => $unit1,
                    'Predetermined_quantity_unit_Dispose' => $unit2,
                    'Predetermined_price_unit_Dispose' => $unit2
                ]);
        }



        $cwInformationPermissionAll = DB::table('cw_information_permission')->select()->get()->toArray();
        foreach ($cwInformationPermissionAll as $cwInformationPermission) {
            $cwInformationPermissionId = $cwInformationPermission -> id;
            $masterUnit = DB::table('master_data')->where('master_id',$cwInformationPermission -> Disposal_quantity_unit)->select()->get()->toArray();
            $masterUnitDis = DB::table('master_data')->where('master_id',$cwInformationPermission -> Planned_quantity_unit)->select()->get()->toArray();
            $unit1 = '';
            $unit2 = '';
            if (!empty($masterUnit)) {
                $unit1 = $masterUnit[0]->master_data;
            }
            if (!empty($masterUnitDis)) {
                $unit2 = $masterUnitDis[0]->master_data;
            }
            DB::table('cw_information_permission')->where('id','=',$cwInformationPermissionId)->update(
                [
                    'Disposal_quantity_unit' => $unit1,
                    'Planned_quantity_unit' => $unit2,
                    'Contract_unit_price_Disposal_unit' => $unit1,
                    'Contract_unit_price_Collection_transportation_unit' => $unit2
                ]);
        }
//      11/20-------------------------------------------------------------------
        $userCompanyAll = DB::table('user_company')->select()->get()->toArray();
        $masterUnits = DB::table('master_data')->where('master_type',10017)->select()->get()->toArray();
        $insertArr = [];
        foreach ($userCompanyAll as $userCompany) {
            $userCompanyId = $userCompany -> id;
            foreach ($masterUnits as $units) {
                $unit = $units -> master_data;
                $arrInsertData = [
                    'user_company_id' => $userCompanyId,
                    'units' => $unit,
                    'create_user_id' => '101',
                    'created_at' => now(),
                    'update_user_id' => '101',
                    'updated_at' => now(),
                ];
                array_push($insertArr,$arrInsertData);
            }
        }
        DB::table('user_company_units_tbl')->insert($insertArr);

//      ------------------------------------------------------------------------------
//        $cw_final_disposal_contentsAll = DB::table('cw_final_disposal_contents')->select()->get()->toArray();
//        foreach ($cw_final_disposal_contentsAll as $cw_final_disposal_contents) {
//            $cw_final_disposal_contentsId = $cw_final_disposal_contents -> id;
//            $reclaimed_item = $cw_final_disposal_contents -> reclaimed_item;
//            if (!empty($reclaimed_item)) {
//                $masterData = DB::table('master_data')->where('master_id','=',$reclaimed_item)->select('master_data')->get()->toArray();
//                if (!empty($masterData)) {
//                    DB::table('cw_final_disposal_contents')->where('id','=',$cw_final_disposal_contentsId)
//                        ->update(['reclaimed_item' => $masterData[0]->master_data]);
//                }
//            }
//        }
//        $cw_partyd_reclaimed_itemAll = DB::table('cw_partyd_reclaimed_item')->select()->get()->toArray();
//        foreach ($cw_partyd_reclaimed_itemAll as $cw_partyd_reclaimed_item) {
//            $cw_partyd_reclaimed_itemId = $cw_partyd_reclaimed_item -> id;
//            $Waste_type = $cw_partyd_reclaimed_item -> Waste_type;
//            if (!empty($Waste_type)) {
//                $masterData = DB::table('master_data')->where('master_id','=',$Waste_type)->select('master_data')->get()->toArray();
//                if (!empty($masterData)) {
//                    DB::table('cw_partyd_reclaimed_item')->where('id','=',$cw_partyd_reclaimed_itemId)
//                        ->update(['Waste_type' => $masterData[0]->master_data]);
//                }
//            }
////        }
//        DB::table('master_data')->insert(array (
//            0 =>
//                array (
//                    'master_id' => '10331',
//                    'master_type' => '10017',
//                    'master_data' => '回',
//                    'display_order' => '10011',
//                    'remarks' => 'WDS',
//                    'create_user_id' => '101',
//                    'created_at' => '2020-01-29 17:03:36',
//                    'update_user_id' => '101',
//                    'updated_at' => '2020-01-29 17:03:36',
//                ),
//            1 =>
//                array (
//                    'master_id' => '10332',
//                    'master_type' => '10017',
//                    'master_data' => 'なし',
//                    'display_order' => '10012',
//                    'remarks' => 'WDS',
//                    'create_user_id' => '101',
//                    'created_at' => '2020-01-29 17:03:36',
//                    'update_user_id' => '101',
//                    'updated_at' => '2020-01-29 17:03:36',
//                )
//            )
//        );
//        DB::table('master_data')->where('master_type','=',10010)->where('display_order','=',10004)->update(['master_data' => '＊３水銀含有ばいじん等']);
//        DB::table('master_data')->insert(array (
////            0 =>
////                array (
////                    'master_id' => '10285',
////                    'master_type' => '10017',
////                    'master_data' => '台',
////                    'display_order' => '10009',
////                    'remarks' => 'WDS',
////                    'create_user_id' => '101',
////                    'created_at' => '2020-01-29 17:03:36',
////                    'update_user_id' => '101',
////                    'updated_at' => '2020-01-29 17:03:36',
////                ),
//            0 =>
//                array (
//                    'master_id' => '10330',
//                    'master_type' => '10017',
//                    'master_data' => '車',
//                    'display_order' => '10010',
//                    'remarks' => 'WDS',
//                    'create_user_id' => '101',
//                    'created_at' => '2020-01-29 17:03:36',
//                    'update_user_id' => '101',
//                    'updated_at' => '2020-01-29 17:03:36',
//                )
//        ));
//        $contractDataAll = DB::table('contract_ready_info')->select()->get()->toArray();
//        foreach ($contractDataAll as $contractData) {
//            $contractDataId = $contractData -> id;
//            $contractDataNum = $contractData -> contract_Num;
//            $controlNumber = $contractData -> control_number;
//            if (empty($controlNumber)) {
//                DB::table('contract_ready_info')->where('id','=',$contractDataId)->update(['control_number' => $contractDataNum]);
//            }
//        }
//
//        $allPermission = DB::table('cw_information_permission')
//            ->join('cw_permission_item', 'cw_information_permission.CW_permission_item_id', '=','cw_permission_item.id')
//            ->select('cw_information_permission.*','cw_permission_item.order_text','cw_permission_item.Harmful_distinction','cw_permission_item.master_data_master_id')
//            ->wherenotnull('waste_type')->get()->toArray();
//        $map = [];
//        for ($i = 0; $i< sizeof($allPermission); $i++) {
//            $permissionId = $allPermission[$i]->id;
//            if (!empty($allPermission[$i])) {
//                $waste_type = $allPermission[$i]->waste_type;
//                $master_data_id = $allPermission[$i]->master_data_master_id;
//                if (!is_numeric($waste_type)) {
////                    $allPermission[$i]['waste_type'] = $master_data_id;
//                    $map['waste_type'] = $master_data_id;
//                    DB::table('cw_information_permission')->where('id',"$permissionId")->update($map);
//                }
//            }
//        }
//        $allDisposalPermission = DB::table('cw_contract')
//            ->join('contract_ready_info', 'contract_ready_info.id', '=', 'cw_contract.contract_ready_info_id')
//            ->join('cw_contents_outsourcing_business', 'cw_contents_outsourcing_business.CW_contract_id', '=', 'cw_contract.id')
//            ->join('cw_permission_item', 'cw_permission_item.CW_Contents_outsourcing_business_id', '=', 'cw_contents_outsourcing_business.id')
//            ->join('cw_information_permission', 'cw_information_permission.CW_permission_item_id', '=', 'cw_permission_item.id')
//            ->where('contract_ready_info.contract_type', '=', 1)
//            ->where('contract_ready_info.contract_classification_type_master_id', '=', 10210)
//            ->select('cw_information_permission.*')
//            ->get()->toArray();
//
//        for ($k = 0; $k < sizeof($allDisposalPermission); $k++) {
//            if (!empty($allDisposalPermission[$k])) {
//                $permissionId = $allDisposalPermission[$k]->id;
//                $Planned_quantity = $allDisposalPermission[$k]->Planned_quantity;
//                $Planned_quantity_unit = $allDisposalPermission[$k]->Planned_quantity_unit;
//                if (empty($allDisposalPermission[$k]->Disposal_quantity)) {
//                    $mapDisposal['Disposal_quantity'] = $Planned_quantity;
//                }
//                if (empty($allDisposalPermission[$k]->Planned_quantity_unit)) {
//                    $mapDisposal['Disposal_quantity_unit'] = $Planned_quantity_unit;
//                }
//                $mapDisposal['Planned_quantity'] = '';
//                $mapDisposal['Planned_quantity_unit'] = '';
//                DB::table('cw_information_permission')->where('id', "$permissionId")->update($mapDisposal);
//            }
//        }
//        $allDisposalPermissionCol = DB::table('cw_contract')
//            ->join('contract_ready_info', 'contract_ready_info.id', '=', 'cw_contract.contract_ready_info_id')
//            ->join('cw_contents_outsourcing_business', 'cw_contents_outsourcing_business.CW_contract_id', '=', 'cw_contract.id')
//            ->join('cw_permission_item', 'cw_permission_item.CW_Contents_outsourcing_business_id', '=', 'cw_contents_outsourcing_business.id')
//            ->join('cw_information_permission', 'cw_information_permission.CW_permission_item_id', '=', 'cw_permission_item.id')
//            ->where('contract_ready_info.contract_type', '=', 1)
//            ->where('contract_ready_info.contract_classification_type_master_id', '=', 10211)
//            ->select('cw_information_permission.*')
//            ->get()->toArray();
//
//        for ($j = 0; $j < sizeof($allDisposalPermissionCol); $j++) {
//            if (!empty($allDisposalPermission[$j])) {
//                $permissionId = $allDisposalPermission[$j]->id;
//                $Planned_quantity_unit = $allDisposalPermission[$j]->Planned_quantity_unit;
//                if (empty($allDisposalPermission[$j]->Planned_quantity_unit)) {
//                    $mapDisposalCol['Disposal_quantity_unit'] = $Planned_quantity_unit;
//                    DB::table('cw_information_permission')->where('id', "$permissionId")->update($mapDisposalCol);
//                }
//            }
//        }
//        DB::table('contract_ready_info')->where('contract_type', 0)->update(['contract_Kind_Name' => '産業廃棄物委託処理契約書']);
//        DB::table('contract_ready_info')->where('contract_type', 1)->update(['contract_Kind_Name' => '建設廃棄物委託処理契約書']);
//        DB::table('contract_ready_info')->where('contract_type', 3)->update(['contract_Kind_Name' => '事業系一般廃棄物処理委託契約書']);

//        $userCompany = DB::table('user_company')->select('id')->get()->toArray();
//        for ($k = 0; $k < sizeof($userCompany); $k++) {
//            if (!empty($userCompany[$k])) {
//                $userCompanyId = $userCompany[$k]->id;
//                $res = DB::table('contract_manager')
//                    ->select('Contract_Manager.id', 'Contract_Manager.users_id')
//                    ->leftJoin('users', 'users.id', '=', 'Contract_Manager.users_id')
//                    ->leftJoin('user_company', 'user_company.id', '=', 'users.User_Company_id')
//                    ->where('user_company.id', '=', $userCompanyId)
//                    ->get()->toArray();
//                if (empty($res)) {
//                    $usersRes = DB::table('users')
//                        ->where('User_Company_Id', '=', $userCompanyId)
//                        ->where('Login_User_Role_id', '=', 103)
//                        ->get()->toArray();
//                    if (!empty($usersRes)) {
//                        $usersID = $usersRes[0]->id;
//                        $mapDisposal['users_id'] = $usersID;
//                        $mapDisposal['create_user_id'] = $usersID;
//                        DB::table('contract_manager')->update($mapDisposal);
//                    }
//                }
//            }
//        }
    }
}
